package com.poh.controller;

import com.poh.pojo.ChoiceQuestion;
import com.poh.pojo.Course;
import com.poh.service.AnswerQuestionService;
import com.poh.service.ChoiceQuestionService;
import com.poh.service.QuestionBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class ChoiceQuestionController {

    @Autowired
    ChoiceQuestionService choiceQuestionService;
    @Autowired
    QuestionBankService questionBankService;
    @Autowired
    AnswerQuestionService answerQuestionService;

    @GetMapping("/deleteChoiceQuestion/{cqId}/{questionBankId}")
    public String deleteChoiceQuestion(@PathVariable("cqId") int cqId,
                                       @PathVariable("questionBankId") String questionBankId,
                                       HttpSession session) {
        Course course = (Course) session.getAttribute("course");
        choiceQuestionService.delectChoiceQuestionByCqId(cqId);
        if (choiceQuestionService.getChoiceQuestionByQuestionBankId(questionBankId).isEmpty() && answerQuestionService.queryAnswerQuestionByQuestionBankId(questionBankId).isEmpty()) {
            questionBankService.deleteQuestionBank(questionBankId);
            return "redirect:/getHomework/" + course.getCourseId();
        }
        return "redirect:/intoHomework/" + questionBankId;
    }

    @GetMapping("/deleteChoiceQuestion/{cqId}")
    public String deleteAddChoiceQuestion(@PathVariable("cqId") int cqId) {
        choiceQuestionService.delectChoiceQuestionByCqId(cqId);
        return "redirect:/toAddQuestionBank";
    }
    @PostMapping("/addChoiceQuestion")
    public String addChoiceQuestion(ChoiceQuestion choiceQuestion, HttpSession session) {
        String questionBankId = (String) session.getAttribute("questionBankId");
        choiceQuestion.setQuestionBankId(questionBankId);
        choiceQuestionService.addChoiceQuestion(choiceQuestion);
        return "redirect:/toAddQuestionBank";
    }

    @PostMapping("/updateInfoChoiceQuestion")
    public String updateInfoChoiceQuestion(ChoiceQuestion choiceQuestion) {
        choiceQuestionService.updateChoiceQuestionById(choiceQuestion);
        return "redirect:/intoHomework/" + choiceQuestion.getQuestionBankId();
    }
    @PostMapping("/updateChoiceQuestion")
    public String updateChoicQuestion(ChoiceQuestion choiceQuestion, Model model) {
//        ChoiceQuestion oldChoiceQuestion = choiceQuestionService.queryChoiceQuestionById(choiceQuestion.getCqId());
//        model.addAttribute("oldChoiceQuestion", oldChoiceQuestion);
        choiceQuestionService.updateChoiceQuestionById(choiceQuestion);
        return "redirect:/toAddQuestionBank";
    }
}
